﻿<%@ Page Title="" Language="C#" MasterPageFile="~/UI/Main.Master" AutoEventWireup="true" CodeBehind="Khohang.aspx.cs" Inherits="QLBH.UI.Khohang" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <style type="text/css">
        .popover {
            width: 276px;
        }

        .popover-title {
            padding: 0px;
        }


        .popover-content {
            max-height: 276px;
            overflow: auto;
        }

        table th {
            font-size: 16px;
        }

        #table_quyen th {
            font-size: inherit;
        }

        table tr td {
            text-align: left;
        }
    </style>


    <div class="row">
        <div class="col-md-4">
        </div>
        <div class="col-md-4 text-right" style="padding-bottom: 5px;">
            <span id="btn_refresh" class="btn btn-success"><span class="glyphicon glyphicon-refresh"></span></span>
            <span id="btn_add" data-toggle="popover" class="btn btn-primary">&nbsp<span class="glyphicon glyphicon-plus"></span></span>
            <span id="btn_edit" data-toggle="popover" class="btn btn-primary"><span class="glyphicon glyphicon-pencil"></span></span>
            <span id="btn_delete" data-toggle="popover" class="btn btn-danger"><span class="glyphicon glyphicon-trash"></span></span>

        </div>
        <div class="col-md-4 text-right">
            <div class="form-group">
                <input type="search" class="form-control" id="txt_search" placeholder="Nhập vào tên kho hàng" />
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12 text-center" id="lazyScroll" style="overflow: auto; max-height: 310px;">

            <table id="table_kho" class="table table-bordered " style="min-width: 800px;">
                <thead>
                    <tr>
                        <th class="text-center" style="width: 30px;">
                            <input id="chk_check_all" type="checkbox" value="-1" />
                        </th>
                        <th class="text-center" style="width: 50px;">STT</th>
                        <th style="width: 100px;">Mã kho</th>
                        <th>Tên kho </th>
                        <th>Địa chỉ</th>
                        <th>Điện thoại</th>
                        <th>Email</th>
                        <th>Fax</th>
                        <th>Mô tả</th>
                    </tr>
                </thead>
                <tbody>
                    <%-- <tr>
                        <td>
                            <input type="checkbox" value="" />
                        </td>
                        <td>1</td>
                        <td>Mark</td>
                        <td>Otto</td>
                        <td>@mdo</td>
                    </tr>--%>
                </tbody>
            </table>
        </div>
    </div>

    <div id="popoverExampleTwoHiddenContent" style="display: none">

        <form role="form">
            <label id="lbl_lan_sua_cuoi" class="hidden"></label>
            <div class="form-group">
                <label for="txt_ma_kho">Mã kho</label>
                <input type="text" class="form-control" name="txt_ma_kho" id="txt_ma_kho" placeholder="Mã kho" />
            </div>

            <div class="form-group">
                <label for="txt_ten_kho">Tên kho</label>
                <input type="text" class="form-control" name="txt_ten_kho" id="txt_ten_kho" placeholder="Tên kho" />
            </div>

            <div class="form-group">
                <label for="txt_dia_chi">Địa chỉ</label>
                <textarea rows="2" class="form-control" name="txt_dia_chi" id="txt_dia_chi" placeholder="Địa chỉ"></textarea>
            </div>

            <div class="form-group">
                <label for="txt_dien_thoai">Điện thoại</label>
                <input type="tel" class="form-control" name="txt_dien_thoai" id="txt_dien_thoai" placeholder="Điện thoại" />
            </div>

            <div class="form-group">
                <label for="txt_email">Email</label>
                <input type="email" class="form-control" name="txt_email" id="txt_email" placeholder="Email" />
            </div>


            <div class="form-group">
                <label for="txt_fax">Fax</label>
                <input type="text" class="form-control" name="txt_fax" id="txt_fax" placeholder="Fax" />
            </div>

            <div class="form-group">
                <label for="txt_mo_ta">Mô tả</label>
                <textarea rows="2" class="form-control" name="txt_mo_ta" id="txt_mo_ta" placeholder="Mô tả"></textarea>
            </div>

            <button id="btn_save" type="submit" class="btn btn-primary pull-right">&nbsp&nbsp Lưu &nbsp&nbsp  </button>
        </form>

    </div>


    <!-- Popover 2 hidden title -->
    <div id="popoverExampleTwoHiddenTitle" style="display: none;">
        <h4 class="text-center">Cập nhật kho hàng</h4>
    </div>


    <script src="slidebars/scripts/bootstrap/js/jquery.min.js"></script>
    <script src="slidebars/scripts/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            $("#lazyScroll").scroll(function () {
                if ($(this).scrollTop() + $(this).innerHeight() >= $(this)[0].scrollHeight) {
                    //BlockUI();
                    //var tables = document.getElementById("table_kho");
                    //var search_term = $("#txt_search").val();
                    //TimKhoHang(search_term, tables.rows.length - 1);
                }
            });


            function Kiemtrahople() {
                if ($('#txt_ten_kho').val().length <= 0) {
                    $('#txt_ten_kho').focus();
                    $('#txt_ten_kho').tooltip({ placement: 'top', title: 'Bạn cần nhập tên kho' });
                    return false;
                }

                if ($('#txt_dia_chi').val().length <= 0) {
                    $('#txt_dia_chi').focus();
                    $('#txt_dia_chi').tooltip({ placement: 'top', title: 'Bạn cần nhập địa chỉ' });
                    return false;
                }

                if ($('#txt_dien_thoai').val().length <= 0) {
                    $('#txt_dien_thoai').focus();
                    $('#txt_dien_thoai').tooltip({ placement: 'top', title: 'Bạn cần nhập điện thoại' });
                    return false;
                }

                return true;
            }

            function XoaText() {
                $('#txt_ma_kho').val('');
                $('#txt_ten_kho').val('');
                $('#txt_dia_chi').val('');
                $('#txt_dien_thoai').val('');
                $('#txt_email').val('');
                $('#txt_fax').val('');
                $('#txt_mo_ta').val('');
            }

            function BlockUI() {
                $.blockUI({
                    message: "<img src='images/ajax-loader.gif'/>", css: { left: '50%', cursor: 'wait', backgroundColor: 'none', border: 'none', width: '50px' },
                    overlayCSS: { backgroundColor: 'none', opacity: 0.0, cursor: 'wait' }
                });
            }

            function BlockUIPopover() {
                $('.popover').block({
                    message: "<img src='images/ajax-loader.gif'/>", css: { left: '50%', cursor: 'wait', backgroundColor: 'none', border: 'none', width: '50px' },
                    overlayCSS: { backgroundColor: 'none', opacity: 0.0, cursor: 'wait' }
                });
            }

            function TimKhoHang(ten_kho_hang, tong_so_dong) {

                var DTO = {
                    'ten_kho_hang': ten_kho_hang
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/TimKhoHangTheoTen",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {

                        $.each($.parseJSON(data.d), function (idx, obj) {
                            tong_so_dong += 1;
                            $("#table_kho tbody").append(
                            '<tr>' +
                            ' <td>' +
                            ' <input type="checkbox" value="' + tong_so_dong + '" />' +
                            ' </td>' +
                            ' <td>' + tong_so_dong + '</td>' +
                            ' <td>' + obj.MA_KHO_HANG + '</td>' +
                            ' <td>' + obj.TEN_KHO_HANG + '</td>' +
                            ' <td>' + obj.DIA_CHI + '</td>' +
                            ' <td>' + obj.DIEN_THOAI + '</td>' +
                            ' <td>' + obj.EMAIL + '</td>' +
                            ' <td>' + obj.FAX + '</td>' +
                            ' <td>' + obj.MO_TA + '</td>' +
                            ' <td class ="hidden"> ' + obj.NGUOI_SUA + '</td>' +
                            ' <td class ="hidden">' + obj.LAN_SUA_CUOI + '</td>' +
                            ' </tr>');
                        });

                    },
                    error: function (data) {
                        console.log(data.d);
                    }

                });
            }

            function TimQuyen(ma_nguoi_dung, ma_chuc_nang) {

                BlockUIPopover();
                var DTO = {
                    'ma_nguoi_dung': ma_nguoi_dung,
                    'ma_chuc_nang': ma_chuc_nang
                };

                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/LayQuyenTheoMaND",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $(".popover").unblock();
                        var obj = $.parseJSON(data.d)
                        var OK = false;
                        if ((obj.SO_QUYEN & 2) == 2) {
                            OK = true;
                            $("#chk_edit").prop('checked', 'checked');
                        }

                        if ((obj.SO_QUYEN & 4) == 4) {
                            OK = true;
                            $("#chk_delete").prop('checked', 'checked');
                        }

                        if ((obj.SO_QUYEN & 8) == 8) {
                            OK = true;
                            $("#chk_view").prop('checked', 'checked');
                        }

                        if ((obj.SO_QUYEN & 1) == 1) {
                            OK = true;
                            $("#chk_addnew").prop('checked', 'checked');
                        }

                        if (!OK) {
                            $("#chk_edit").prop('checked', '');
                            $("#chk_delete").prop('checked', '');
                            $("#chk_view").prop('checked', '');
                            $("#chk_addnew").prop('checked', '');
                        }
                    },
                    error: function (data) {
                        $(".popover").unblock();
                        console.log(data.d);
                    }

                });
            }

            onload = function () {
                BlockUI();
                var tables = document.getElementById("table_kho");
                TimKhoHang('', tables.rows.length - 1);
            }

            function Themoikhohang() {
                if (!Kiemtrahople()) {
                    return false;
                }
                BlockUIPopover();
                var KHO = {};
                KHO.MA_KHO_HANG = $('#txt_ma_kho').val();
                KHO.TEN_KHO_HANG = $('#txt_ten_kho').val();
                KHO.DIA_CHI = $('#txt_dia_chi').val();
                KHO.DIEN_THOAI = $('#txt_dien_thoai').val();
                KHO.EMAIL = $('#txt_email').val();
                KHO.FAX = $('#txt_fax').val();
                KHO.MO_TA = $('#txt_mo_ta').val();
                KHO.NGUOI_SUA = '';


                var DTO = {
                    'KHO': KHO
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/ThemMoiKho",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $(".popover").unblock();
                        var obj = jQuery.parseJSON(data.d);
                        if (obj.SUCCESS == -1) {
                            MakeToast('danger', 'Lỗi', obj.ERROR);
                        }
                        else {
                            MakeToast('success', 'Thành công', obj.ERROR);
                            XoaText();
                            $("#table_kho tbody").html('');
                            var tables = document.getElementById("table_kho");
                            var search_term = $("#txt_search").val();
                            TimKhoHang(search_term, tables.rows.length - 1);
                        }

                    },
                    error: function (data) {
                        $(".popover").unblock();
                    }

                });
            }

            function Capnhatkhohang() {
                if (!Kiemtrahople()) {
                    return false;
                }
                BlockUIPopover();
                var KHO = {};
                KHO.MA_KHO_HANG = $('#txt_ma_kho').val();
                KHO.TEN_KHO_HANG = $('#txt_ten_kho').val();
                KHO.DIA_CHI = $('#txt_dia_chi').val();
                KHO.DIEN_THOAI = $('#txt_dien_thoai').val();
                KHO.EMAIL = $('#txt_email').val();
                KHO.FAX = $('#txt_fax').val();
                KHO.MO_TA = $('#txt_mo_ta').val();
                KHO.NGUOI_SUA = '';
                KHO.LAN_SUA_CUOI = $("#lbl_lan_sua_cuoi").html();

                var DTO = {
                    'KHO': KHO
                };

                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/CapNhatKho",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $(".popover").unblock();
                        var obj = jQuery.parseJSON(data.d);
                        if (obj.SUCCESS == -1) {
                            MakeToast('danger', 'Lỗi', obj.ERROR);
                        }
                        else {
                            MakeToast('success', 'Thành công', obj.ERROR);
                            $("#table_kho tbody").html('');
                            var tables = document.getElementById("table_kho");
                            var search_term = $("#txt_search").val();
                            TimKhoHang(search_term, tables.rows.length - 1);
                        }

                    },
                    error: function (data) {
                        $(".popover").unblock();
                    }

                });
            }

            function Xoakho() {

                BlockUIPopover();
                var ma_kho_hang = [];

                var tables = document.getElementById("table_kho");
                var k = -1;
                for (var i = 1; i < tables.rows.length; i++) {
                    var row = tables.rows[i]
                    if (row.className == 'info') {
                        k++;
                        ma_kho_hang[k] = row.cells[2].innerHTML;
                    }
                }

                var DTO = {
                    'ma_kho_hang': ma_kho_hang
                };
                $.ajax({
                    type: "POST",
                    url: "../BUSINESS/Services.asmx/XoaKho",
                    data: JSON.stringify(DTO),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        $(".popover").unblock();
                        var obj = jQuery.parseJSON(data.d);
                        if (obj.SUCCESS == -1) {
                            MakeToast('danger', 'Lỗi', obj.ERROR);
                        }
                        else {
                            MakeToast('success', 'Thành công', obj.ERROR);
                            $("#btn_delete").popover('hide');
                            $("#table_kho tbody").html('');
                            var tables = document.getElementById("table_kho");
                            var search_term = $("#txt_search").val();
                            TimKhoHang(search_term, tables.rows.length - 1);
                        }
                    },
                    error: function (data) {
                        $(".popover").unblock();
                    }

                });
            }

            $('body').on('click', 'span#btn_ok', function (event) {
                event.preventDefault();
                Xoakho();
            });


            $('body').on('click', 'button#btn_save', function (event) {
                event.preventDefault();
                if ($('#txt_ma_kho').is(':disabled')) {
                    Capnhatkhohang();
                }
                else
                    Themoikhohang();
            });


            $("#btn_add").click(function (event) {
                event.preventDefault();

            });

            function CheckTableNguoidung() {
                var tables = document.getElementById("table_kho");
                var k = -1;
                for (var i = 0; i < tables.rows.length; i++) {
                    var row = tables.rows[i]
                    if (row.className == 'info') {
                        k = i;
                        break;
                    }
                }
                return k;
            }

            $("#btn_edit").click(function (event) {
                event.preventDefault();
                var k = CheckTableNguoidung();
                if (k != -1) {
                    $(this).popover('show');
                }
            });


            $("#btn_delete").click(function (event) {
                event.preventDefault();
                var tables = document.getElementById("table_kho");
                var k = CheckTableNguoidung();
                if (k != -1) {
                    $(this).popover('show');
                }
            });

            $("#btn_refresh").click(function (event) {
                event.preventDefault();
                BlockUI();
                $("#table_kho tbody").html('');
                var tables = document.getElementById("table_kho");
                var search_term = $("#txt_search").val();
                TimKhoHang('', tables.rows.length - 1);
            });


            var table = document.getElementById("table_kho");

            $("table").on("click", ":checkbox", function () {
                if ($(this).val() == -1) {
                    if ($(this).is(':checked')) {
                        $("table tr").addClass("info");
                        $("table tr:first").removeClass("info");
                    }
                    else
                        $("table tr").removeClass("info");
                    return;
                }

                if ($(this).is(':checked')) {
                    $(this).parents("tr:first").addClass("info");
                }
                else {
                    $(this).parents("tr:first").removeClass("info");
                }
            });

            $("#chk_check_all").change(function () {

                if (this.checked) {
                    var table = document.getElementById("table_kho");
                    $('td input:checkbox', table).prop('checked', this.checked);
                }
                else {
                    var table = document.getElementById("table_kho");
                    $('td input:checkbox', table).prop('checked', '');
                }
            });

            $("#txt_search").keyup(function (e) {
                if (e.keyCode == 13) {
                    e.preventDefault();
                    e.stopPropagation();
                    var search_term = $("#txt_search").val();
                    if (search_term != '') {
                        BlockUI();
                        $("#table_kho tbody").html('');
                        var tables = document.getElementById("table_kho");
                        var search_term = $("#txt_search").val();
                        TimKhoHang(search_term, tables.rows.length - 1);
                    }
                }

            });

            $("#btn_add").popover({
                html: true,
                content: function () {
                    return $('#popoverExampleTwoHiddenContent').html();
                },
                title: function () {
                    return $('#popoverExampleTwoHiddenTitle').html();
                },
                placement: 'bottom'

            });

            $("#btn_edit").popover({
                html: true,
                content: function () {
                    return $('#popoverExampleTwoHiddenContent').html();
                },
                title: function () {
                    return $('#popoverExampleTwoHiddenTitle').html();
                },
                placement: 'bottom',
                trigger: 'Click'

            });

            $("#btn_delete").popover({
                html: true,
                content: function () {

                    return $('#popoverConfirm').html();
                },
                title: function () {
                    return '<h4 class="text-center">Xác nhận xóa</h4>';
                },
                placement: 'bottom',
                trigger: 'Click'

            });

            $('#btn_add').on('shown.bs.popover', function () {
                var h = $(window).height() - 239;
                $(".popover-content").css({ 'max-height': h });
            });

            $('#btn_edit').on('shown.bs.popover', function () {
                var tables = document.getElementById("table_kho");
                var k = -1;
                for (var i = 0; i < tables.rows.length; i++) {
                    var row = tables.rows[i]
                    if (row.className == 'info') {
                        $('#txt_ma_kho').attr("disabled", "disabled")
                        $('#txt_ma_kho').val(row.cells[2].innerHTML);
                        $('#txt_ten_kho').val(row.cells[3].innerHTML);
                        $('#txt_dia_chi').val(row.cells[4].innerHTML);
                        $('#txt_dien_thoai').val(row.cells[5].innerHTML);
                        $('#txt_email').val(row.cells[6].innerHTML);
                        $('#txt_fax').val(row.cells[7].innerHTML);
                        $('#txt_mo_ta').val(row.cells[8].innerHTML);
                        $("#lbl_lan_sua_cuoi").html(row.cells[10].innerHTML);

                        k = i;
                        break;
                    }
                }
                var h = $(window).height() - 239;
                $(".popover-content").css({ 'max-height': h });

            });

            $('#btn_delete').on('shown.bs.popover', function () {
                var tables = document.getElementById("table_kho");
                var k = 0;
                for (var i = 0; i < tables.rows.length; i++) {
                    var row = tables.rows[i]
                    if (row.className == 'info') {
                        k++;
                    }
                }
                if (k != 0) {
                    $("#msgcontent").html('Bạn có chắc muốn xóa <strong>' + k + '</strong> kho ?');
                }
            });


        });

    </script>

</asp:Content>
